//
//  QJSHomePageController.m
//  aEP项目A
//
//  Created by lanou3g on 16/4/15.
//  Copyright © 2016年 qiu. All rights reserved.
//

#import "QJSHomePageController.h"
#import "CK_MenuViewController.h"
#import "QJSNEtWorkRequestManager.h"
#import "QJSCollectionViewCell.h"
#import "QQCycleView.h"
@interface QJSHomePageController ()<MenuClickDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
//CollectionView
@property (nonatomic,strong)UICollectionView *collectionView;
//数据源数组
@property (nonatomic,strong) NSMutableArray *dataSource;
//轮播图属性
@property (strong, nonatomic) QQCycleView *cycleView;
@end

@implementation QJSHomePageController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
       [CK_MenuViewController getMenuViewController].menuDelegate = self;
       [CK_MenuViewController getMenuViewController].menuArray = [@[@"今日・TODAY",@"话题・TOPICS",@"音乐・MUSIC",@"良品・PRODUCTS",@"订阅・SUBSCRITION",@"设置・SETTING"] mutableCopy];
    
    //初始化数据源
    self.dataSource = [NSMutableArray new];
    
  //网络请求
[QJSNEtWorkRequestManager requesURL:[NSURL URLWithString:READLIST_URL] Method:QJS_GET Params:nil Result:^(NSData *data, NSURLResponse *response, NSError *error) {
   
    if (data) {
        NSMutableDictionary *jsonDict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dataDict = jsonDict[@"data"];
        for (NSDictionary *itemDict in dataDict[@"list"]) {
            QJSHomeModel *model = [QJSHomeModel new];
            [model setValuesForKeysWithDictionary:itemDict];
            [self.dataSource addObject:model];
        }
    }
    //回到主线程
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [self.collectionView reloadData];
        

    });
    
}];
    //创建FlowLayout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    //设置Layout
    layout.itemSize = CGSizeMake(self.view.frame.size.width * 0.3, self.view.frame.size.height * 0.25);
    layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 200);

    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.view addSubview:self.collectionView];
    //注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"QJSCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    //注册头
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

    
}
#pragma Mark ------UICollectionViewDelegate
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        //创建头部滚动视图
    self.cycleView = [[QQCycleView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) interVal:3];
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        [header addSubview:self.cycleView];
        
        self.cycleView.imgNameArray = @[@"篮球1.jpg",@"篮球2.jpg",@"篮球3.jpg",@"篮球4.jpg",@"篮球5.jpg",@"篮球6.jpg",@"篮球7.jpg",@"篮球8.jpg"].mutableCopy;
        
        return header;
    }

    return nil;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    QJSCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}

//点击抽屉按钮
- (IBAction)didClickleftBarBtnItem:(UIBarButtonItem *)sender {
    
    // 判断菜单展示状态
    if ([CK_MenuViewController getMenuViewController].isShowing) {
        // 关闭菜单方法
        [[CK_MenuViewController getMenuViewController] hideLeftViewController];
    }else {
        // 打开菜单方法
        [[CK_MenuViewController getMenuViewController] showLeftViewController];
    }

}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
